/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package test1.problem2;

import java.util.Random;

/**
 *
 * @author hoang.pt
 */
public class Thread1  implements Runnable{

    Thread t;

    Data d;

    /**
     * constructor
     */
    public Thread1(Data d) {

        //init data
        this.d = d;

        //init thread
        t = new Thread(this);
        t.start();
    }

    
    /**
     * 
     */
    public void run()
    {
        //run forever
        while (true) {

            //synchronise
            synchronized(d) {
                
                //check flag
                //if false get random
                if (!d.flag) {
                    //--get random
                    d.getRandom();

                    //--change flag
                    d.flag = true;

                    //--notify
                    d.notify();
                }

                //if true wait
                try {
                    d.wait();
                } catch (InterruptedException ex) {
                }
            }
        }
    }
}
